package cn.tedu.egmvc8.controller;

import cn.tedu.egmvc8.mapper.NoticeMapper;
import cn.tedu.egmvc8.pojo.dto.NoticeAddParam;
import cn.tedu.egmvc8.pojo.dto.NoticeAddQuery;
import cn.tedu.egmvc8.pojo.dto.NoticeUpdateParam;
import cn.tedu.egmvc8.pojo.entity.Notice;
import cn.tedu.egmvc8.pojo.vo.NoticeDetailInfoVO;
import cn.tedu.egmvc8.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/v1/notice/add")
    @ResponseBody
    public String add(NoticeAddParam noticeAddParam) {
        System.out.println(noticeAddParam);

        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(998L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        int num = noticeMapper.insertNotice(notice);

        return num > 0 ? "新增" + num + "条资讯成功！" : "新增咨询失败！";
    }

    @GetMapping("/v1/notice/list")
    @ResponseBody
    public List<NoticeListVO> list(NoticeAddQuery noticeAddQuery) {
        System.out.println(noticeAddQuery);

        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();
        List<NoticeListVO> noticeListVOList = noticeMapper.noticeList(title, type);

        return noticeListVOList;
    }

    @GetMapping("/v1/notice/detail")
    @ResponseBody
    public NoticeDetailInfoVO detail(Long id) {
        System.out.println(id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.noticeDetail(id);

        return noticeDetailInfoVO;
    }

    @PostMapping("/v1/notice/update/{id}")
    @ResponseBody
    public String update(@PathVariable Long id, NoticeUpdateParam noticeUpdateParam) {
        System.out.println(id + "," + noticeUpdateParam);

        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setId(id);
        notice.setUpdateTime(new Date());
        int num = noticeMapper.noticeUpdate(notice);

        return num > 0 ? "修改" + num + "条资讯成功！" : "修改咨询失败！";
    }

    @PostMapping("/v1/notice/delete")
    @ResponseBody
    public String deleteByIds(Long[] ids) {

        int num = noticeMapper.noticeDeleteByIds(ids);
        return num > 0 ? "删除" + num + "条资讯成功！" : "删除咨询失败！";

    }
}
